﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tracker.Models.DTO;
using tracker.Models;
using System.Text;
using tracker.Models.DAL;
using System.Data;

namespace tracker.Controllers
{
    public class HandlerSingleLastLocationController : Controller
    {
        //
        // GET: /HandlerSingleLastLocation/
        [CallAjaxFilter]
        public JsonStringResult Index(string sssid)
        {
            string result = "";
            if (String.IsNullOrEmpty(sssid))
            {
                result = "[]";
            }
            else
            {
                string currentUserGroupID = Session[Common.USER_GROUPID].ToString();
                string securityCode = Session[Common.USER_SECUTIRY_CODE].ToString();
                HandlerLastLocationController lastLocation = new HandlerLastLocationController();
                List<ELastDeviceInfo> listDevice = lastLocation.getList(sssid, currentUserGroupID, securityCode);
                if (listDevice.Count > 0)
                {
                    StringBuilder buildString = new StringBuilder();
                    buildString.Append(Utilities.getJsonString(listDevice, "", 2));
                    buildString.Remove(buildString.ToString().LastIndexOf('}'), 1);
                    buildString.Remove(buildString.ToString().LastIndexOf(']'), 1);
                    buildString.Append(getDefineInfo(listDevice));
                    buildString.Append("}");
                    buildString.Append("]");
                    result = buildString.ToString();
                }
                else
                {
                    result = "[]";
                }
            }
            return new JsonStringResult(result);
        }
        public string getDefineInfo(List<ELastDeviceInfo> listDevice)
        {

            string _username = Session[Common.USER].ToString();

            StringBuilder strReturn = new StringBuilder();
            string State = null;
            foreach (ELastDeviceInfo item in listDevice)
            {
                State = item.State;
            }
            DataTable dt = new StatusDefineDAL().getByUsername(_username);
            State = State == null ? "" : State;
            foreach (DataRow item in dt.Rows)
            {
                for (int i = 0; i < State.Length; i++)
                {
                    if (i == int.Parse(item["BitIndex"].ToString()))
                    {
                        if (State[i] == '0')
                        {
                            strReturn.Append(",\"" + item["Expression"].ToString() + "\":");
                            strReturn.Append("\"" + item["false_exp"] + "\"");
                        }
                        else if (State[i] == '1')
                        {
                            strReturn.Append(",\"" + item["Expression"].ToString() + "\":");
                            strReturn.Append("\"" + item["true_exp"] + "\"");
                        }
                    }
                }
            }
            return strReturn.ToString();
        }
    }
}
